import 'package:flutter/material.dart';
import 'package:loxen/api/http_server/report.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/utils/color.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/widget/my_subassembly.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

// ignore: must_be_immutable
class ReportWidget extends StatelessWidget {
  final String id;
  final ReportType reportType;

  Map<String, String> data = {
    "1": "政治有害",
    "2": "低俗色情",
    "3": "暴力恐怖",
    "4": "赌博",
    "5": "诈骗",
    "6": "侵犯他人",
    "7": "侵犯本人(版权方/原作者)",
    "8": "其他"
  };

  ValueNotifier<bool> currentSelect;
  ValueNotifier<int> currentIndex = ValueNotifier(-1);

  ReportWidget(
    this.id,
    this.reportType, {
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<ValueNotifier<bool>> list = [
      ValueNotifier<bool>(false),
      ValueNotifier<bool>(false),
      ValueNotifier<bool>(false),
      ValueNotifier<bool>(false),
      ValueNotifier<bool>(false),
      ValueNotifier<bool>(false),
      ValueNotifier<bool>(false),
      ValueNotifier<bool>(false),
    ];
    List<Widget> widgets = [];
    for (int i = 0; i < list.length; i++) {
      widgets.add(Padding(
        padding: EdgeInsets.only(top: 80.h),
        child: item(data['${i + 1}'], list[i], i),
      ));
    }
    widgets.add(ValueListenableProvider.value(
        value: currentIndex,
        builder: (context, _) {
          int data = context.watch<int>();
          return Container(
            margin: EdgeInsets.only(top: 140.h, bottom: 200.h),
            width: 559.w,
            height: 96.h,
            child: Material(
              color: data == -1 ? Color(0xFF696C72) : Colours.app_main,
              child: InkWell(
                onTap: data == -1
                    ? null
                    : () {
                        int index = currentIndex.value + 1;
                        Navigator.of(context)
                            .pushNamed(Routes.report_content, arguments: {
                          'name': this.data['$index'],
                          'type': '$index',
                          'id': id,
                          'reportType': reportType,
                        }).then((value) {
                          if (value) {
                            Navigator.pop(context);
                          }
                        });
                      },
                child: Center(
                  child: Text(
                    '举报',
                    style: TextStyle(
                      fontSize: 30.sp,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ),
          );
        }));
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size(double.infinity, 95.h),
        child: Container(
          color: Theme.of(context).backgroundColor,
          child: SafeArea(
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    MySubassembly.getBackIcon(context,
                        color: Theme.of(context).textTheme.subtitle2.color),
                    Expanded(
                      child: Center(
                        child: Text(
                          '举报',
                          style: TextStyle(
                              fontSize: 32.sp,
                              fontWeight: FontWeight.w500,
                              color:
                                  Theme.of(context).textTheme.subtitle2.color),
                        ),
                      ),
                    ),
                    MySubassembly.getSearchIcon(null, text: Text('')),
                  ],
                )
              ],
            ),
          ),
        ),
      ),
      body: Container(
        color: Colors.white,
        child: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.only(left: 94.w, right: 94.w),
            child: Column(
              children: widgets,
            ),
          ),
        ),
      ),
    );
  }

  Widget item(String name, ValueNotifier<bool> data, int index) {
    return ValueListenableProvider.value(
      value: data,
      child: GestureDetector(
        onTap: () {
          currentIndex.value = index;
          if (currentSelect != null) {
            if (currentSelect == data) return;
            currentSelect.value = false;
            data.value = true;
            currentSelect = data;
          } else {
            currentSelect = data;
            data.value = true;
          }
        },
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Expanded(
                  child: Text(
                    name,
                    style: TextStyle(
                      fontSize: 28.sp,
                      color: Colors.black,
                    ),
                  ),
                ),
                Builder(builder: (context) {
                  bool select = context.watch<bool>();
                  return select
                      ? Image.asset(
                          'images/selected.png',
                          width: 36.w,
                          height: 36.w,
                        )
                      : Constants.empty;
                })
              ],
            ),
            Container(
              margin: EdgeInsets.only(top: 19.h),
              color: Color(0xffEBECF1),
              height: 1.h,
            )
          ],
        ),
      ),
    );
  }
}
